package com.cf.data.serve.environment;

import com.cf.data.serve.entity.LbsjkData;
import com.cf.data.serve.service.LbsjkDataService;
import com.cf.data.serve.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Date;

@Slf4j
public class TCPServer_lbsjk {
    private static LbsjkDataService lbsjkDataService = BeanUtils.getBean(LbsjkDataService.class);
    public static void lbsjk(int port) throws IOException {
        ServerSocket serverSocket = null;
        try {
            System.out.println("开启端口：8338");
            serverSocket = new ServerSocket(port); // 监听1234端口
            Socket clientSocket = serverSocket.accept(); // 等待客户端连接
            System.out.println("连接成功");

            // 获取输入流
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            // 获取输出流
            OutputStream outputStream = clientSocket.getOutputStream();
            String inputLine;
            //存储单个2进制字符
            String[] cun = null;
            String parseResult = "";
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
                cun = inputLine.split(";"); // 将接收到的消息发送回客户端
            }

            if(cun!=null){
                LbsjkData lbsjkData = new LbsjkData();
                lbsjkData.setId(String.valueOf(System.currentTimeMillis()));
                lbsjkData.setStatus("0");
                lbsjkData.setCreateTime(new Date());
                for(int i=0;i<=cun.length;i++){
                    System.out.println(cun[i]);
                    String[] z = cun[i].split(":");
                    switch (z[0]){
                        case "DEV_type" :
                            lbsjkData.setDevType(z[1]);
                            break;
                        case "DEV_sn" :
                            lbsjkData.setDevSn(z[1]);
                            break;
                        case "GPS_DEV_J" :
                            lbsjkData.setGpsDevJ(z[1]);
                            break;
                        case "GPS_DEV_W" :
                            lbsjkData.setGpsDevW(z[1]);
                            break;
                        case "CCID" :
                            lbsjkData.setCcid(z[1]);
                            break;
                        case "BAT_DL" :
                            lbsjkData.setBatDl(z[1]);
                            break;
                        case "CSQ" :
                            lbsjkData.setCsq(z[1]);
                            break;
                        case "START" :
                            lbsjkData.setStart(z[1]);
                            break;
                        case "CX_TYP" :
                            lbsjkData.setCxTyp(z[1]);
                            break;
                        case "CX_LN" :
                            lbsjkData.setCxLn(z[1]);
                            break;
                        case "CX_STP" :
                            lbsjkData.setCxStp(z[1]);
                            break;
                        case "CX_SZX" :
                            lbsjkData.setCxSzx(z[1]);
                            break;
                        case "CX_SZY" :
                            lbsjkData.setCxSzy(z[1]);
                            break;
                    }
                }
                lbsjkDataService.save(lbsjkData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (serverSocket != null) ;
            serverSocket.close();
            System.out.println("客户端已断开连接。");
            lbsjk(port);
        }
    }
}
